
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#COMPARISON WITH DATA & ARGENTINE COUNTERFACTUAL
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------

## Upload Argentina data on spreads, inflation misreport and GDP cylce
#-------------------------------------------------------------------------------
 SP_ARG_data       =  readdlm(string(Arg_Data_path,EMBI_data),      ',', Float64)
 InfMisreport_Data =  readdlm(string(Arg_Data_path,Misreport_data), ',', Float64)
 Y_data            =  readdlm(string(Arg_Data_path,Output_data),    ',', Float64)
 Y_data            =  exp.(Y_data[:,1])

#[0] Simulate the economy to pin down initial ζ0
#-------------------------------------------------------------------------------
T=100_000
N=1
y_sim, type_sim, b_sim, π_sim, def_sim, def_pol, def_id, q_sim, SP_sim, SP_HR_sim, c_sim, tb_sim, m_sim, m_sim_ctfl, ζ_sim, dlnSP_sim, dBE_sim, b_sim_tmp, inf_bp,
elasticity, ΔSP_sim,ΔBE_sim,Δπ_pol = simul_econ(ce,T,N, T0=5000)
filter_C       = (type_sim.==1).*(def_sim.==0)
ζ_avg_C        = mean(ζ_sim[filter_C])
b0_val, ζ0_val = 0.68, ζ_avg_C

#[1] Run simulation under I-type, using Argentine GDP data
#-------------------------------------------------------------------------------
T = 50
N = 20_000
#[1] Run simulation under I-type, using Argentine GDP data
y_sim_I, type_sim_I, b_sim_I, π_sim_I, def_sim_I, def_pol_I, def_ind_I, q_sim_I, SP_sim_I, SP_HR_sim_I,c_sim_I, tb_sim_I, m_sim_I, m_sim_ctfl_I, ζ_sim_I, dlnSP_sim_I,  dBE_sim_I, b_sim_tmpI, inf_bp_I  =
simul_econ(ce::CE_Economy_M, T,N; T0=2,    Arg_Ctfl=1, Type_Gov="I", High_Rep=0,    b0=b0_val,ζ0=ζ0_val       )
RP_sim_I = 100*(SP_sim_I-SP_HR_sim_I)./SP_sim_I

#[2] Run simulation under P-type [bond policy of simulation [1]]
#-------------------------------------------------------------------------------
y_sim_P, type_sim_P, b_sim_P, π_sim_P, def_sim_P, def_pol_P, def_ind_P, q_sim_P, SP_sim_P, SP_HR_sim_P, c_sim_P, tb_sim_P, m_sim_P, m_sim_ctfl_P, ζ_sim_P, dlnSP_sim_P,  dBE_sim_P, b_sim_tmpP, inf_bp_P  =
simul_econ(ce::CE_Economy_M, T,N; T0=2,   Arg_Ctfl=1, Type_Gov="P",  High_Rep=0,    b0=b0_val,ζ0=ζ0_val,           b_pol=b_sim_tmpI      )
RP_sim_P = 100*(SP_sim_P-SP_HR_sim_P)./SP_sim_P

#[3] Run simulation under P-type High-Rep [bond policy of simulation [1]]
#-------------------------------------------------------------------------------
y_sim_PHR, type_sim_PHR, b_sim_PHR, π_sim_PHR, def_sim_PHR, def_pol_PHR, def_ind_PHR, q_sim_PHR, SP_sim_PHR, SP_HR_sim_PHR, c_sim_PHR, tb_sim_PHR, m_sim_PHR, m_sim_ctfl_PHR, ζ_sim_PHR, dlnSP_sim_PHR,  dBE_sim_PHR, b_sim_tmpP, inf_bp_PHR  =
simul_econ(ce::CE_Economy_M, T,N; T0=2,    Arg_Ctfl=1, Type_Gov="P", High_Rep=1, b0=b0_val,ζ0=ce.z_grid[end],   b_pol=b_sim_tmpI      )


# Filtrate by sims in which m=L in at least 1 time during 2007.Q1-2008.Q1
#----------------------------------------------------------------------------------
T          =  size(y_sim_I,1)
period     =  collect(2006.0:0.25:2012.75)
id_2007Q1  =  searchsortedfirst(period,2007.0)
filter_obs =  filter_ArgCtfl_fx(period, def_sim_I, m_sim_I, 2007.0:0.25:2008.0, 1)

### Generate averages from the selected paths
#----------------------------------------------------------------------------------
SP_I_avg = filter_average(SP_sim_I, filter_obs); SP_P_avg = filter_average(SP_sim_P, filter_obs); SP_PHR_avg = filter_average(SP_sim_PHR, filter_obs);
ζ_I_avg  = filter_average(ζ_sim_I, filter_obs);  ζ_P_avg  = filter_average(ζ_sim_P, filter_obs);  ζ_PHR_avg  = filter_average(ζ_sim_PHR, filter_obs);
π_I_avg  = filter_average(π_sim_I, filter_obs);  π_P_avg  = filter_average(π_sim_P, filter_obs);  π_PHR_avg  = filter_average(π_sim_PHR, filter_obs);


#----------------------------------------------------------------------------------
#----------------------------------------------------------------------------------
#BASELINE PLOTS
#----------------------------------------------------------------------------------
#----------------------------------------------------------------------------------

## [1] Figure: Misreports, Output
sizeF      = (600, 600)
labels     = ["Data" "S-type"]
line_types = [:dash :solid]
colors     = [:black :blue]

# Misreport Plot [comparison with data]
p1 = plot(period, [InfMisreport_Data  π_I_avg*100], linestyle=line_types, label=labels, color=colors, linewidth=2.5, fg_legend = :transparent, legend=:topright, size=sizeF, xtickfont=font(12), ytickfont=font(12), legendfont=font(12))
vline!(p1, [period[5]], line = (:black, 2.5, :dot), label="")
savefig(p1, "figures/simulations/ArgCtf/pi_data.pdf")

p1 = plot(period, [Y_data], size=sizeF, linestyle=[:solid :dash], label="", color=:blue, linewidth=2., fg_legend = :transparent, legend=:bottomleft,  xtickfont=font(12), ytickfont=font(12), legendfont=font(12))
vline!(p1, [period[5]], line = (:black, 2.5, :dot), label="")
savefig(p1, "figures/simulations/ArgCtf/Y_model_data.pdf")

## [2] Figure: Arg_Ctfls
labels     = ["S-type" "C-type" "High reputation"]
line_types = [:solid :dash :dashdot]
colors     = [:blue :grey :red]

# Spreads: Ctlfs
p1 = plot(period, [SP_I_avg SP_P_avg SP_PHR_avg]*100, linestyle=line_types, label=labels, color=colors, linewidth=2.5, fg_legend = :transparent, legend=:topright, size=sizeF, xtickfont=font(12), ytickfont=font(12), legendfont=font(12))
vline!(p1, [period[5]], line = (:black, 2.5, :dot), label="")
ylims!(p1,(SP_I_avg[1]*100,1650))
savefig(p1, "figures/simulations/ArgCtf/SP_counterfs.pdf")

# Reputation
p1 = plot(period, [ζ_I_avg ζ_P_avg ζ_PHR_avg], linestyle=line_types, label=labels, color=colors, linewidth=2.5, ylim=(0.05,ce.z_grid[end]), fg_legend = :transparent, legend=:topright, size=sizeF, xtickfont=font(12), ytickfont=font(12), legendfont=font(12))
vline!(p1, [period[5]], line = (:black, 2.5, :dot), label="")
savefig(p1, "figures/simulations/ArgCtf/z_counterfs.pdf")

# Spreads: Model vs Data
p1 = plot(period, [SP_ARG_data/100 mean(SP_sim_I[:,filter_obs],2)]*100, linestyle=[:dash :solid], label=["Data" "S-type"], color=[:black :blue], linewidth=2.5, fg_legend = :transparent, legend=:topright, size=sizeF, xtickfont=font(12), ytickfont=font(12), legendfont=font(12))
vline!(p1, [period[5]], line = (:black, 2.5, :dot), label="")
savefig(p1, "figures/simulations/ArgCtf/SP_model_quants.pdf")
#------------------------------------------------------------------------------





################################################################################
#Comparison with Perfect Information Case
################################################################################

#LOAD & SIMULATE PERFECT INFORMATION CASE
#-------------------------------------------------------------------------------
include("../02_Perfect_Information/01_Model_Structure.jl")
include("../02_Perfect_Information/02_Functions.jl")
include("../02_Perfect_Information/10_Simulations_Main_fx.jl")
name_file   = string("../02_Perfect_Information/model_data/CE_model_PerfInfo.jld");
ce_PI       = load(string(name_file), "ce")

#Run simulation under I-type for the Perfect Information case
#-------------------------------------------------------------------------------
T=50
y_sim_I_PI, type_sim_I_PI, b_sim_I_PI, π_sim_I_PI, def_sim_I_PI, def_pol_I_PI, def_ind_I_PI, q_sim_I_PI, SP_sim_I_PI, SP_HR_sim_I_PI, c_sim_I_PI, tb_sim_I_PI, b_sim_tmpI_PI, VF_sim_I  =
simul_econ_PI(ce_PI, T, N, T0=2, Counterfactual=1, Type_Gov="I", b0=b0_val)
RP_sim_I_PI = 100*(SP_sim_I_PI - SP_HR_sim_I_PI)./SP_sim_I_PI
RP_I_avg_PI = mean(RP_sim_I_PI,2)
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
#Filter Observations based on m=L paths
#-------------------------------------------------------------------------------
sample= Vector{Array{Float64,1}}([])
push!(sample,  period)                      # [1] no filter -- all obs
push!(sample,  period[id_2007Q1:end])       # [2] no m=L during 2007.Q1-2012.Q4
last_period=2007.25
for jj=1:size(period,1)-id_2007Q1
push!(sample,  collect(2007.0:0.25:last_period))    #at least 1 m=L during 2007.Q1-last period
last_period = last_period+0.25
end
NmL =[-1; 0; ones(size(sample,1)-2)]

#Select the base path: 2007.Q1-2008.Q1
base_path   =  6


#Compute averages across the different filters
#-------------------------------------------------------------------------------
RP_I_avg = zeros(size(period,1),size(sample,1))

for i=1:size(sample,1)
    filter_obs     = filter_ArgCtfl_fx(period, def_sim_I, m_sim_I, sample[i], NmL[i])
    #S-type case
    RP_I_avg[:,i]  = filter_average(RP_sim_I,filter_obs)
end

#Compute percentiles based on all paths (unfiltered)
#-------------------------------------------------------------------------------
filter_obs     = filter_ArgCtfl_fx(period, def_sim_I, m_sim_I, sample[1], NmL[1])

#Percentiles [with filter of at least one m=L during the sample period]
T_tmp = size(RP_sim_I,1)
size_tmp=99
RP_I_perc    = zeros(T_tmp, size_tmp)
for t=1:T_tmp
for perc_id=1:size_tmp
RP_I_perc[t,perc_id]    = percentile(RP_sim_I[t,filter_obs], perc_id)
end
end
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------

#Plot: Reputation Premium - Comparison with Perfect Information
sizeF=(600,600)
#RP [alternative paths and CI]
plot(period,   [RP_I_avg[:,base_path]],             linestyle=:solid, color=:blue,   linewidth=2.0,  fg_legend = :transparent, legend=:topright, size=sizeF, xtickfont=font(12), ytickfont=font(12), legendfont=font(11),label="Baseline Model")
plot!(period,  [RP_I_perc[:,5]],  fillrange = [RP_I_perc[:,95]], fillalpha = 0.15, alpha=0.15, c = "grey", legendfont=font(11), label="90% CI")
plot!(period,  [RP_I_avg_PI   ],                    linestyle=:dash, color=:black,  linewidth=2.0,  fg_legend = :transparent, size=sizeF, xtickfont=font(12), ytickfont=font(12), legendfont=font(11),label="Perfect Information")
ylims!(-3,60)
vline!([2007], line = (:black, 2.5, :dot), label="")
savefig("figures/simulations/ArgCtf/RP_AlternativePahts.pdf")